<template>
  <div id="taskCheckList">
    <!--上部搜索条件组件-->
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <div class="table-conBtn">
      <el-button type="warning" icon="el-icon-check" @click="passWork">
        通过
      </el-button>
      <el-button type="warning" icon="el-icon-close" plain @click="notPass">
        不通过
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
      <selectColumn
        ref="selectCum"
        @setColumn="setColumns"
        :nameList="columnList"
      ></selectColumn>
    </div>
    <!--下部table-->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
      max-height="490"
      ref="table"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        :selectable="selectAble"
      ></el-table-column>
      <el-table-column type="index" width="50" label="序号">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.avatar"
        label="头像"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <listImgString :imgInfo="scope.row.topic.user.avatar"></listImgString>
        </template>
      </el-table-column>
      <el-table-column
        label="昵称"
        v-if="columnSHow.name"
        prop="topic.user.name"
        min-width="60"
      ></el-table-column>
      <el-table-column
        label="大鹏号"
        v-if="columnSHow.dpAccount"
        prop="topic.user.dpAccount"
        width="70"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.topic.user.dpAccount">
            {{ scope.row.topic.user.dpAccount }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column label="类型" v-if="columnSHow.jobType" min-width="70">
        <template slot-scope="scope">
          <span
            v-if="scope.row.ext.label && scope.row.ext.label.jobType"
            class="tag-span"
            :class="scope.row.ext.label.jobType | filterClass('jobWorkType')"
            >{{ scope.row.ext.label.jobType | filterName('jobWorkType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="作业标题"
        v-if="columnSHow.taskTitle"
        prop="jobInfo.taskTitle"
        min-width="80"
      ></el-table-column>
      <el-table-column
        label="作业码"
        v-if="columnSHow.identCode"
        prop="jobInfo.identCode"
        min-width="80"
      ></el-table-column>
      <el-table-column
        label="作业性质"
        v-if="columnSHow.courseType"
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.jobInfo.courseType === 'VIP'">正式作业</span>
          <span v-if="scope.row.jobInfo.courseType === 'TEST'">试学作业</span>
        </template>
      </el-table-column>
      <el-table-column
        label="课程标题"
        v-if="columnSHow.courseName"
        prop="jobInfo.courseName"
        min-width="80"
      ></el-table-column>
      <el-table-column
        label="文字内容"
        v-if="columnSHow.content"
        width="80"
        class-name="text-overflow"
      >
        <template slot-scope="scope">
          <span
            :title="scope.row.content"
            v-if="scope.row.content"
            class="textContent"
            v-html="filterContent(scope.row)"
            @click="showDetail(scope.row)"
            >{{ scope.row.content }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="视图预览"
        v-if="columnSHow.images"
        min-width="100"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.images && scope.row.images.length > 0">
            <listImgShow :imgInfo="scope.row.images"></listImgShow>
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="点赞数"
        v-if="columnSHow.likeCount"
        prop="commentInfo.likeCount"
        min-width="70"
      >
        <template slot-scope="scope">
          <span v-if="!scope.row.commentInfo">0</span>
          <span v-else>{{ scope.row.commentInfo.likeCount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="回复数"
        v-if="columnSHow.replyCount"
        prop="commentInfo.replyCount"
        min-width="70"
      >
        <template slot-scope="scope">
          <span v-if="!scope.row.commentInfo">0</span>
          <span v-else>{{ scope.row.commentInfo.replyCount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="评论时间"
        v-if="columnSHow.createdDate"
        prop="createdDate"
        min-width="80"
      ></el-table-column>
      <el-table-column
        label="用户属性"
        v-if="columnSHow.userProperty"
        prop="userProperty"
        width="80"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.userProperty"
            class="tag-span"
            :class="scope.row.userProperty | filterClass('checkUserAttr')"
            >{{ scope.row.userProperty | filterName('checkUserAttr') }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="机审时间"
        v-if="columnSHow.machineAuditTime"
        prop="machineAuditTime"
        width="80"
      ></el-table-column>
      <el-table-column
        label="机审结果"
        v-if="columnSHow.machineAuditStatus"
        prop="machineAuditStatus"
        width="80"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.machineAuditStatus"
            class="tag-span"
            :class="scope.row.machineAuditStatus | filterClass('result')"
            >{{ scope.row.machineAuditStatus | filterName('result') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="人工审核结果"
        v-if="columnSHow.peopleAuditStatus"
        width="110"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.peopleAuditStatus"
            class="tag-span"
            :class="scope.row.peopleAuditStatus | filterClass('result')"
            >{{ scope.row.peopleAuditStatus | filterName('result') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="审核人"
        v-if="columnSHow.auditingUser"
        show-overflow-tooltip
        prop="auditingUser.name"
        width="70"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.auditingUser.name">{{
            scope.row.auditingUser.name
          }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="人工审核时间"
        v-if="columnSHow.peopleAuditTime"
        show-overflow-tooltip
        prop="peopleAuditTime"
        width="110"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.peopleAuditTime">{{
            scope.row.peopleAuditTime
          }}</span>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="190">
        <template slot-scope="scope">
          <el-button
            v-permission="'wh:system:taskCommentExamined:task'"
            type="text"
            @click="showDetail(scope.row)"
          >
            作业详情
          </el-button>
          <el-button
            v-permission="'wh:system:taskCommentExamined:comment'"
            type="text"
            @click="changeView(scope.row.ext.originTopicId)"
          >
            评论列表
          </el-button>
          <el-button
            v-if="scope.row.peopleAuditStatus === 3"
            v-permission="'wh:system:taskCommentExamined:adopt'"
            type="text"
            @click="handlPassWork(scope.row.id)"
          >
            通过
          </el-button>
          <el-button
            v-if="scope.row.peopleAuditStatus === 3"
            v-permission="'wh:system:taskCommentExamined:noadopt'"
            type="text"
            @click="handlNotPassWord(scope.row.id)"
          >
            不通过
          </el-button>
          <el-button
            v-if="scope.row.peopleAuditStatus === 2"
            type="text"
            @click="showReason(scope.row)"
          >
            不通过理由
          </el-button>
          <el-button
            v-if="scope.row.peopleAuditStatus === 1"
            v-permission="'wh:system:taskCommentExamined:noadopt'"
            type="text"
            @click="handlNotPassWord(scope.row.id)"
          >
            删除
          </el-button>
          <template
            v-if="scope.row.commentInfo && scope.row.peopleAuditStatus !== 2"
          >
            <el-button
              v-permission="'wh:system:taskCommentExamined:commentTop'"
              v-if="
                !scope.row.commentInfo.firstCommentId &&
                  !scope.row.commentInfo.isRecommend
              "
              type="text"
              @click="toTop(scope.row.commentInfo.id)"
            >
              置顶
            </el-button>
            <el-button
              v-permission="'wh:system:taskCommentExamined:commentTop'"
              type="text"
              @click="cancelTop(scope.row.commentInfo.id)"
              v-if="
                !scope.row.commentInfo.firstCommentId &&
                  scope.row.commentInfo.isRecommend
              "
              >取消置顶</el-button
            >
            <el-button
              v-permission="'wh:system:taskCommentExamined:commentAddLike'"
              type="text"
              @click="showAddPraise(scope.row)"
            >
              加赞
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <dialogDetailCom
      v-if="dialogData.isShow"
      :dialog-data="dialogData"
    ></dialogDetailCom>
    <add-praise-dialog
      v-if="addPraiseData.isShow"
      :add-praise-data="addPraiseData"
      @refresh="refresh"
    ></add-praise-dialog>
    <del-dialog
      :del-dialog-data="delDialogData"
      @reasonDel="failRequest"
    ></del-dialog>
  </div>
</template>

<script>
import topSearch from '@component/commentCheck/jobCommentCheck/topSearch' // 上部搜索条件
import dialogDetailCom from '@common/dialogDetailCom' // 详情-弹窗-公共组件
import addPraiseDialog from '@common/dialogAddPraise' // 加赞弹窗
import delDialog from '@common/delDialog' // 不通过理由弹窗
import { filterCon, getCreateTime, validateOneMonth } from '@utils/pubFunction'
import { saveScrollMixins } from '@/mixins/saveScroll'
import { columnMixin } from '@/mixins/columnShowHide'
import listImgString from '@common/listImgString' // 视图预览(返回值为字符串结构)-公共组件
import {
  getSearchList,
  passSingleAPI,
  passMultAPI,
  failMultRequestAPI
} from '@/api/commentCheck/jobComment'
import { recommend, cRecommend } from '@/api/common/commentsAPI'
export default {
  name: 'discussCheck',
  msg: '评论审核-作业评论审核-列表页',
  mixins: [saveScrollMixins, columnMixin],
  components: {
    topSearch,
    dialogDetailCom,
    addPraiseDialog,
    delDialog,
    listImgString
  },
  data() {
    return {
      loading: true,
      total: 0,
      tableData: [], // table内容
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      formInline: {
        // 搜索条件
        peopleAuditStatus: '', // 人审结果
        machineAuditStatus: '', // 机审结果
        sort: '', // 顺序
        timeType: '', // 顺序2
        checkUserId: '', // 审核人
        searchText: '', // 查询评论内容
        userProperty: '', // 用户属性
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        createTime: [],
        userType: 'dpAccount',
        searchUserContent: '',
        jobType: ''
      },
      dialogData: {
        isShow: false, // 默认隐藏
        type: '', // 类型，workDetail-作品详情，jobDetail-作业详情
        id: '', // 编辑时，传递的参数
        title: ''
      },
      multipleSelection: [],
      addPraiseData: {
        isShow: false,
        likeCount: ''
      },
      delDialogData: {
        isShow: false,
        params: '',
        type: '不通过'
      },
      id: '',
      columnList: [
        { label: '头像', value: 'avatar' },
        { label: '昵称', value: 'name' },
        { label: '大鹏号', value: 'dpAccount' },
        { label: '类型', value: 'jobType' },
        { label: '作业标题', value: 'taskTitle' },
        { label: '作业码', value: 'identCode' },
        { label: '作业性质', value: 'courseType' },
        { label: '课程标题', value: 'courseName' },
        { label: '文字内容', value: 'content' },
        { label: '视图预览', value: 'images' },
        { label: '点赞数', value: 'likeCount' },
        { label: '回复数', value: 'replyCount' },
        { label: '评论时间', value: 'createdDate' },
        { label: '用户属性', value: 'userProperty' },
        { label: '机审时间', value: 'machineAuditTime' },
        { label: '机审结果', value: 'machineAuditStatus' },
        { label: '人工审核结果', value: 'peopleAuditStatus' },
        { label: '审核人', value: 'auditingUser' },
        { label: '人工审核时间', value: 'peopleAuditTime' }
      ]
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    this.getCreateTime()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
        this.$refs['selectCum'].setChoose(this.columnAry)
      }, 1000)
    })
  },
  methods: {
    /* 获取列表 */
    searchList() {
      let params = { ...this.searchParams, ...this.formInline }
      delete params.createTime
      if (!params.startTime || !params.endTime) {
        this.$message.warning('时间范围不允许为空')
        return
      }
      if (!validateOneMonth(params.startTime, params.endTime)) {
        this.$message.warning('时间范围须在一个月内')
        return
      }
      this.loading = true
      getSearchList(
        params,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    selectAble(row, index) {
      if (row.peopleAuditStatus === 3) {
        return true
      } else {
        return false
      }
    },
    /* 过滤返回评论内容 */
    filterContent(item) {
      return filterCon(item)
    },
    /* table选择 */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /* 跳转页面 */
    changeView(id) {
      this.$router.push({
        name: 'wh:commentCheck:jobCommentCheck:jobCommentCheckSecondList',
        query: {
          topicId: id
        }
      })
    },
    /* 显示详情 */
    showDetail(item) {
      this.dialogData.type = 'jobDetail'
      this.dialogData.id = item.ext.originTopicId
      this.dialogData.title = '作业评论审核详细'
      this.dialogData.isShow = true
    },
    /* 通过 */
    passWork() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      this.$confirm(
        `确认要审核通过选中的${this.multipleSelection.length}条作品评论审核信息吗？`,
        '确定提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let ids = []
        this.multipleSelection.forEach((item) => {
          ids.push(item.id)
        })
        this.multPassRequest(ids)
      })
    },
    // 通过单个
    handlPassWork(id) {
      this.$confirm('确认通过吗？', '通过提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.singlelPassRequest(id)
      })
    },
    // 通过单条请求
    singlelPassRequest(id) {
      passSingleAPI(id, (response) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.searchList()
      })
    },
    // 通过批量请求
    multPassRequest(ids) {
      let params = {
        ids
      }
      passMultAPI(params, (response) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.searchList()
      })
    },
    /* 批量不通过 */
    notPass() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      let ids = []
      this.multipleSelection.forEach((item) => {
        ids.push(item.id)
      })
      this.id = ids
      this.delDialogData.isShow = true
    },
    /* 不通过单个 */
    handlNotPassWord(id) {
      this.id = [id]
      this.delDialogData.isShow = true
    },
    failRequest(reason) {
      this.failMultRequest(this.id, reason)
      this.delDialogData.isShow = false
    },
    // 不通过多个请求
    failMultRequest(ids, reason) {
      let type = '不通过'
      this.$confirm(
        `${type}理由：${reason} 确认${type}该条信息？`,
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let params = {
          ids,
          reason
        }
        failMultRequestAPI(params, (response) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.searchList()
        })
      })
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    /* 新增编辑后，刷新列表 */
    refresh() {
      this.searchList()
    },
    getCreateTime() {
      const createTime = getCreateTime()
      this.formInline.startTime = createTime[0]
      this.formInline.endTime = createTime[1]
      this.formInline.createTime = createTime
    },
    showReason(row) {
      this.$alert(`${row.reason}`, '未通过理由', {
        showConfirmButton: false
      }).catch(() => {})
    },
    toTop(commentId) {
      this.$confirm('确认要置顶该评论吗？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = await recommend(commentId)
          if (res) {
            this.searchList()
          }
        })
        .catch(() => {})
    },
    cancelTop(commentId) {
      this.$confirm('确认要取消置顶该评论吗?', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = await cRecommend(commentId)
          if (res) {
            this.searchList()
          }
        })
        .catch(() => {})
    },
    showAddPraise(row) {
      let likeCount = row.commentInfo.likeCount
      let commentId = row.commentInfo.id
      this.addPraiseData.likeCount = likeCount
      this.addPraiseData.commentId = commentId
      this.addPraiseData.isShow = true
    }
  }
}
</script>

<style lang="less">
#taskCheckList {
  @import url('~@common/style/imgBox.less');
  @import url('~@common/style/clamp.less');
  .textContent {
    cursor: pointer;
  }
}
</style>
